//
//  JCEmotionKeyboard.swift
//  SinaWeibo_18
//
//  Created by 魏欣宇 on 15/10/30.
//  Copyright © 2015年 cz. All rights reserved.
//  表情键盘

import UIKit

private let CELL_ID = "EMOTION_CELL"

class JCEmotionKeyboard: UIView, JCEmotionToolBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource {

    override init(frame: CGRect) {
        super.init(frame: frame)
        //表情键盘的背景颜色
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        addSubview(toolBar)
        addSubview(collectionView)
        addSubview(pageControl)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        //设置toolbar的  大小
        toolBar.width = width
        toolBar.height = 35
        toolBar.y = height - toolBar.height
        
        //设置collectionView的 大小
        collectionView.size = CGSizeMake(width, toolBar.y)
        //设置collectionView  item的大小
        let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = collectionView.size
        
        //设置pagecontrol的位置
        pageControl.centerX = width * 0.5
        pageControl.y = toolBar.y - 10
        
    }
    // MARK : JCEmotionToolBarDelegate   //判断点击的时toolbar的那个按钮
    func emotionToolBar(toolBar: JCEmotionToolBar, buttonClickType: JCEmotionToolBarButtonType) {
        
        //获得当前表情在第几页
        let page = JCEmotionTools.pageNumWithEmotionType(buttonClickType)
        
        //要滚动到得位置
        let indexpath = NSIndexPath(forRow: page, inSection: 0)
        //滚动
        collectionView.scrollToItemAtIndexPath(indexpath, atScrollPosition: UICollectionViewScrollPosition.None, animated: false)

    }
    //底部切换不同表情的toolbar
    private lazy var toolBar: JCEmotionToolBar = {
            let toolBar  = JCEmotionToolBar()
        
        toolBar.delegate = self
        
        return toolBar
        }()
    //现实表情的collectionView
    private lazy var collectionView: UICollectionView = {
        //指定布局
        let layout =  UICollectionViewFlowLayout()
        //指定滑动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置水平方向间隔
        layout.minimumLineSpacing = 0
        
        let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clearColor()
        
        collectionView.delegate = self
        collectionView.dataSource = self
        //设置分页
        collectionView.pagingEnabled = true
        //隐藏水平滚动条
        collectionView.showsHorizontalScrollIndicator = false
        
        //注册 系统的 collectionView cell
        collectionView.registerClass(JCEmotionPageCell.self, forCellWithReuseIdentifier: CELL_ID)
        
        return collectionView
    }()
    //分页控件
    private lazy var pageControl: UIPageControl = {
        let pageControl = UIPageControl()
        //指定不同状态下的颜色
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        pageControl.pageIndicatorTintColor = UIColor.grayColor()
        
        return pageControl
    }()
    
}


// MARK : - 维护collectionview的delegate和datasource
extension JCEmotionKeyboard{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //返回多少页
        return JCEmotionTools.emotionsTotalPageCount()
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(CELL_ID, forIndexPath: indexPath) as! JCEmotionPageCell
       
        
        //设置cell的indexpath ,隐藏最近label
        cell.indexPath = indexPath
        
        //设置表情数据
        cell.emotions = JCEmotionTools.subEmotionsWithPageNum(indexPath.row)
        
        return cell
    }
    //根据滚动页确定表情
    func scrollViewDidScroll(scrollView: UIScrollView) {
    
        //计算滚动到第几页
        let page = Int(scrollView.contentOffset.x / scrollView.width + 0.5)
        //判断范围 得到表情类型
        let type = JCEmotionTools.emotionType(page)
        //选中按钮
        toolBar.selectedButtonWithType(type)
        
        if page == 0 {
            pageControl.hidden = true
        }else{
            pageControl.hidden = false
        //求出当前page对应的表情类型的第几页
            //当前类型总页数
        let pageControlNumPages = JCEmotionTools.pageControlTotalPages(type)
        pageControl.numberOfPages = pageControlNumPages
            //计算当前滚动到某种表情第几页
        let pageControlCurrentPage = JCEmotionTools.pageControlCurrentPage(page)
        pageControl.currentPage = pageControlCurrentPage
        }
    }
    
}
